﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Linq;
using PPFinance.Controls;
using System.ComponentModel;



namespace PPFinance.Controls
{
    public class ClosingEventArgs : EventArgs
    {
        public bool IsCancel { get; set; }
    }
    public class PPTabItem : System.Windows.Controls.TabItem
    {


        public bool CanClosed
        {
            get { return (bool)GetValue(CanCloseProperty); }
            set { SetValue(CanCloseProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CanClose.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CanCloseProperty =
            DependencyProperty.Register("CanClosed", typeof(bool), typeof(PPTabItem), new PropertyMetadata(new PropertyChangedCallback(CanCloseChanged)));


        static public void CanCloseChanged(object DependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var cMenu = (DependencyObject as PPTabItem).ContextMenu;
            if (cMenu != null)
            {
                (DependencyObject as PPTabItem).ContextMenuChange(cMenu, (bool)e.NewValue);
            }
        }

        public string HeaderText { get; private set; }
        public object HeaderControl { get; private set; }
        public string GroupName { get; set; }
        public delegate void ClosingHandler(object sender, ClosingEventArgs e);
        public event ClosingHandler OnClosing;
        public delegate void OpenNewHandler(PPTabItem item);
        public event OpenNewHandler OnOpenNew;
        public ContextMenu ContextMenu { get; set; }
        protected override void OnHeaderChanged(object oldHeader, object newHeader)
        {
            if (string.IsNullOrEmpty(HeaderText))
            {
                CreateHeader();
                this.Header = HeaderText;
            }
            var btn = (HeaderControl as Grid).Children[1] as PPTabCloseButton;
            if (this.CanClosed)
            {
                btn.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                btn.Visibility = System.Windows.Visibility.Collapsed;
            }
            base.OnHeaderChanged(oldHeader, newHeader);
        }


        public PPTabItem()
        {

            CanClosed = true;
            this.Loaded += new RoutedEventHandler(PPTabItem_Loaded);
            ContextMenu = getContextMenu();

        }
        void PPTabItem_Loaded(object sender, RoutedEventArgs e)
        {
            ContextMenuChange(this.ContextMenu, CanClosed);
            ContextMenuService.SetContextMenu(this, ContextMenu);
        }
        void CreateHeader()
        {
            HeaderText = this.Header.ToString();
            Grid grHeader = new Grid();
            grHeader.Margin = new Thickness(0, 0, -9, 0);
            grHeader.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
            grHeader.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(25, GridUnitType.Pixel) });
            TextBlock tbHeader = new TextBlock() { Text = HeaderText };
            grHeader.Children.Add(tbHeader);
            Grid.SetColumn(tbHeader, 0);
            var btnClose = new PPTabCloseButton();
            btnClose.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            grHeader.Children.Add(btnClose);
            Grid.SetColumn(btnClose, 1);
            HeaderControl = grHeader;
            btnClose.Click += new RoutedEventHandler(btnClose_Click);

        }

        void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }


        public void ContextMenuChange(ContextMenu cMenu, bool canClosed)
        {

            var items = cMenu.Items.Where(p => (p as MenuItem).Name != "cmOtherClose");
            foreach (var item in items)
            {
                if (canClosed)
                {
                    (item as MenuItem).IsEnabled = true;
                }
                else
                {
                    (item as MenuItem).IsEnabled = false;
                }
            }
        }

        private ContextMenu getContextMenu()
        {
            ContextMenu cm = new ContextMenu();
            var cmCurrentClose = new MenuItem() { Header = "关闭选项卡", Name = "cmCurrentClose" };
            var cmGroupClose = new MenuItem() { Header = "关闭选项卡组", Name = "cmGroupClose" };
            var cmOtherClose = new MenuItem() { Header = "关闭其他选项卡", Name = "cmOtherClose" };
            var cmOpen = new MenuItem() { Header = "新增此类型选项卡", Name = "cmOpen" };
            if (!this.CanClosed)
            {
                cmCurrentClose.IsEnabled = false;
            }
            cmCurrentClose.Click += new RoutedEventHandler(cmCurrentClose_Click);
            cmOpen.Click += new RoutedEventHandler(cmOpen_Click);
            cmOtherClose.Click += new RoutedEventHandler(cmOtherClose_Click);
            cmGroupClose.Click += new RoutedEventHandler(cmGroupClose_Click);
            cm.Items.Add(cmCurrentClose);
            cm.Items.Add(cmOpen);
            cm.Items.Add(cmGroupClose);
            cm.Items.Add(cmOtherClose);
            return cm;
        }

        void cmGroupClose_Click(object sender, RoutedEventArgs e)
        {
            var tab = this.Parent as PPTabControl;
            tab.CloseGroup(this);
        }

        void cmOpen_Click(object sender, RoutedEventArgs e)
        {
            OnOpenNew(this);
        }

        void cmOtherClose_Click(object sender, RoutedEventArgs e)
        {
            var tab = this.Parent as PPTabControl;
            tab.CloseAllBut(this);
        }

        void cmCurrentClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }


        /// <summary>
        /// 关闭
        /// </summary>
        public void Close()
        {
            if (OnClosing != null)
            {
                var args = new ClosingEventArgs() { IsCancel = true };
                OnClosing(this, args);
                if (args.IsCancel)
                {
                    Remove_Item();
                }
            }
            else
            {
                Remove_Item();

            }
        }

        void Remove_Item()
        {
            var tab = this.Parent as TabControl;
            ContextMenuService.SetContextMenu(this, null);
            try
            { tab.Items.Remove(this); }
            catch { }
            tab.UpdateLayout();
        }


    }
}
